package com.zeropoint.demo.controller;

import com.github.pagehelper.PageInfo;
import com.zeropoint.demo.pojo.MenuPO;
import com.zeropoint.demo.pojo.ResultEntity;
import com.zeropoint.demo.pojo.UserPO;
import com.zeropoint.demo.pojo.vo.UserVO;
import com.zeropoint.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultEntity login(@RequestParam("userName") String userName,@RequestParam("passWord") String passWord){

        try {
            // 登录成功下发 token
            UserPO userPO = new UserPO(userName, passWord);
            HashMap<String, Object> map = userService.userLogin(userPO);

            return ResultEntity.successWithData(map);


        }catch (Exception e){
            return ResultEntity.falseWithData(e.getMessage(),e.getMessage());
        }
    }

    @PostMapping("/get/user/info")
    public ResultEntity<UserVO> getUserInfo(){

        try {
            // 根据 token 获取用户信息
            UserVO userVO = userService.selectUserInfo();
            return ResultEntity.successWithData(userVO);
        }catch (Exception e){
            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }




    @GetMapping("/get/menus")
    public ResultEntity<List<MenuPO>> getMenus(){
        // 需要接收 token
        try {

            List<MenuPO> menus = userService.getMenus();
            return ResultEntity.successWithData(menus);
        }catch (Exception e){
            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }


    @GetMapping("/get/user")
    public ResultEntity<PageInfo<UserVO>> getUsers(@RequestParam("currentPage")Integer currentPage,
                                                         @RequestParam("pageSize")Integer pageSize){
        // 需要认证
        try {
            PageInfo<UserVO> users = userService.getUsers(currentPage, pageSize);
            return ResultEntity.successWithData(users);
        }catch (Exception e){
            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }

    @RequestMapping("/unauthorized/{msg}")
    public ResultEntity<String> getError(@PathVariable("msg") String msg){

        try {
            return ResultEntity.falseWithoutData(msg);
        }catch (Exception e){
            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }


    @GetMapping("/get/region")
    public ResultEntity<List<String>> getRegions(String regionName){
        // 需要认证
        try {
            List<String> list = userService.getRegions(regionName);
            return ResultEntity.successWithData(list);
        }catch (Exception e){
            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }


    @PostMapping("/create/user")
    public ResultEntity<UserVO> saveUser(@RequestBody UserVO userVO){
        // 需要认证
        try {
            UserVO userVO1 = userService.saveUser(userVO);
            return ResultEntity.successWithData(userVO1);
        }catch (Exception e){

            if(e instanceof DuplicateKeyException) return ResultEntity.falseWithoutData("用户名已经存在！");
            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }

    @PostMapping("/modify/user/information")
    public ResultEntity<String> modifyUserInformation(@RequestBody HashMap<String,String> map){
        // 需要认证
        try {
            String token = userService.modifyUserInformation(map);
            return ResultEntity.successWithData(token);
        }catch (Exception e){
            return ResultEntity.falseWithoutData(e.getMessage());
        }
    }

    @PostMapping("/modify/user/state")
    public ResultEntity<String> modifyUserState(Integer otherId,String way){
        // 需要认证
        try {
            userService.modifyUserState(otherId,way);
            return ResultEntity.successWithoutData();
        }catch (Exception e){

            return ResultEntity.falseWithoutData(e.getMessage());
        }

    }

    @GetMapping("/get/user/information")
    public ResultEntity<UserVO> getUserInformation(Integer userId){
        // 需要认证
        try {
            UserVO userVO = userService.getUserInformation(userId);
            return ResultEntity.successWithData(userVO);
        }catch (Exception e){

            return ResultEntity.falseWithoutData(e.getMessage());
        }
    }



}
